package cn.cvs.controller;

import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysRoleService;
import cn.cvs.utils.Constants;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class SysRoleController {

    //角色
    @Autowired
    private SysRoleService sysRoleService;


     // 查询角色信息
    @RequestMapping("/sys/role/list")
    public String findByAccountOrRoleId( Model model) {
        List<SysRole> roleList = sysRoleService.findAllSysRole(); // 获取所有角色列表
        model.addAttribute("roleList", roleList); // 将角色列表添加到模型
        return "sysRole/list"; // 返回角色列表视图
    }



    @RequestMapping("/sys/role/simpleList")
    public  ResponseEntity<String> role() {
        List<SysRole> roleList = sysRoleService.findAllSysRole(); // 获取所有角色列表
        String data = JSON.toJSONString(roleList);
        return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(data);
    }



    // 添加角色信息
    @RequestMapping("/sys/role/toAdd")
    public  String toAdd( ){
        return "sysRole/add";
    }

    // 判断角色编码是否已存在
    @RequestMapping(value = "/sys/role/codeExist",produces="application/json;charset=UTF-8")
    @ResponseBody
    public  String codeExist(String code,Map<String,Object> map){
         int i  = sysRoleService.getByCode(code);


         if ( code == null || code.trim().equals("")){
             map.put("exist",-1);
             map.put("msg","该角色编号不能为空");

         } else {
             if ( i > 0){
                 map.put("exist",1);
             } else  if ( i == 0){
                 map.put("exist",0);
             }
         }
         String data = JSON.toJSONString(map);
        return data;
    }

    // 添加角色信息
    @RequestMapping("/sys/role/add")
    public  String addSysRole(SysRole sysRole, HttpSession session){

        // 创建人id
        sysRole.setCreatedUserId(((SysUser)session.getAttribute(Constants.USER_SESSION)).getId());// 获取创建人id

        int i = sysRoleService.addSysRole(sysRole);
        System.out.println(i);
        return "redirect:/sys/role/list";
    }


    // 根据编号删除角色信息
    @RequestMapping(value="/sys/role/del/{id}",produces="application/json;charset=UTF-8")
    @ResponseBody
    public  String delSysRole(@PathVariable("id") Integer id,Map<String,Object> map ){
        String delResult = null;
        try {
            int sysRoleCounts = sysRoleService.sysRoleCount(id);
            if (sysRoleCounts > 0){
                delResult = String.valueOf(sysRoleCounts);
                delResult = "false";
            } else {
                int  count = sysRoleService.delSysRole(id);
                delResult = "false";
                if ( count > 0){
                    delResult = "true";
                } else  if ( count == 0){
                    delResult = "notexist";
                }
            }

        } catch (Exception exception) {
            exception.printStackTrace();
        }

        map.put("delResult",delResult);
        String data = JSON.toJSONString(map);
        return data;

    }

    // 根据编号获取数据

    @RequestMapping("/sys/role/toUpdate/{id}")
    public String findByAccountOrRoleIds(@PathVariable("id") Integer id, Model model) {
        SysRole sysRole = sysRoleService.getBySysRoleOrId(id);
        model.addAttribute("sysRole",sysRole);
        return "sysRole/update";
    }


    // 修改
    @RequestMapping("/sys/role/update")
    public String updateSysRole(SysRole  sysRole,HttpSession session) {

        // 修改人id
        sysRole.setUpdatedUserId(((SysUser)session.getAttribute(Constants.USER_SESSION)).getId());// 获取创建人id
        // 修改时间
        sysRole.setUpdatedTime(new Date());

        int count = sysRoleService.updateSysRole(sysRole);
        if ( count > 0 ){
            return "redirect:/sys/role/list";
        }
        return "sysRole/update";
    }


}
